package com.xxxx.order.service.impl;

import com.xxxx.api.enums.OrderStatus;
import com.xxxx.api.enums.Paystatus;
import com.xxxx.api.enums.PromTypeStatus;
import com.xxxx.api.enums.SendStatus;
import com.xxxx.api.pojo.Admin;
import com.xxxx.api.pojo.Cart;
import com.xxxx.api.pojo.CartResult;
import com.xxxx.common.result.BaseResult;
import com.xxxx.order.mapper.OrderGoodsMapper;
import com.xxxx.order.mapper.OrderMapper;
import com.xxxx.order.pojo.Order;
import com.xxxx.order.pojo.OrderGoods;
import com.xxxx.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Lionel
 * @Date 2020/11/7 2:17
 * Created by IntelliJ IDEA 2020.1.
 * Project: EgoShop
 * Description：
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderGoodsMapper orderGoodsMapper;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Value("${redis.order.increment}")
    private String redisOrderIncrement;

    /**
     * 生成订单
     * @param admin
     * @param cartResult
     * @return
     */
    @Override
    public BaseResult orderSave(Admin admin, CartResult cartResult) {
        //创建order对象
        Order order = new Order();
        //订单编号 shop_年月日时分秒_自增key
        String orderSn = "shop_" + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now()) + "_" + getIncrement(redisOrderIncrement);
        order.setOrderSn(orderSn);
        //用户id
        order.setUserId(admin.getAdminId().intValue());
        //订单状态
        order.setOrderStatus(OrderStatus.no_confirm.getStatus());
        //发货状态
        order.setShippingStatus(SendStatus.no_fhuo.getStatus());
        //支付状态
        order.setPayStatus(Paystatus.no_pay.getStatus());
        //商品总价
        order.setGoodsPrice(cartResult.getTotalPrice());
        //应付金额
        order.setOrderAmount(cartResult.getTotalPrice());
        //订单总价
        order.setTotalAmount(cartResult.getTotalPrice());
        //下单时间
        Long addTime = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        order.setAddTime(addTime.intValue());
        //主表存完了
        int result = orderMapper.insertSelective(order);
        if (result>0) {
            List<OrderGoods> orderGoodsList=new ArrayList<>();
            for (Cart cart : cartResult.getCartList()) {
                OrderGoods orderGoods = new OrderGoods();
                //订单id
                orderGoods.setOrderId(order.getOrderId());
                //商品id
                orderGoods.setGoodsId(cart.getGoodsId());
                //商品名称
                orderGoods.setGoodsName(cart.getGoodsName());
                //商品价格
                orderGoods.setGoodsPrice(cart.getMarketPrice());
                //商品数量
                orderGoods.setGoodsNum(cart.getGoodsNum().shortValue());
                //订单方式
                orderGoods.setPromType(PromTypeStatus.normal.getStatus());
                //发货状态
                orderGoods.setIsSend(SendStatus.no_fhuo.getStatus());
                //添加到订单商品列表
                orderGoodsList.add(orderGoods);
            }
            //批量插入
            result= orderGoodsMapper.insertOrderGoodsBatch(orderGoodsList);
            if (result>0) {
                BaseResult baseResult = BaseResult.success();
                //返回了订单编号
                baseResult.setMessage(orderSn);
                return baseResult;
            }

        }
        return BaseResult.error();
    }

    /**
     * Redis自增key
     * @param key
     * @return
     */
    private Long getIncrement(String key){
        RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        return entityIdCounter.getAndIncrement();
    }
}
